package com.etc.controller;
import com.etc.pojo.mail.Mail;
import com.etc.service.MailSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.util.List;


@Controller
@RequestMapping("mail")
public class MailController {


    @Autowired
    private MailSerivce mailSerivce;

    @RequestMapping(value = "send",method = RequestMethod.POST)
    public void sendMail03(
            @RequestParam(value = "message") String message,
            @RequestParam(value = "active") String active,
            @RequestParam(value = "touser") String touser,PrintWriter out) {

        try {
            Integer integer = mailSerivce.send(message,active,touser);
            if (integer == 1) {
                out.print(1);
            } else {
                out.print(2);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    @RequestMapping("list")
    public String list(HttpServletRequest request) {
        List<Mail> byAll = mailSerivce.mailAll();
        request.setAttribute("maillist",byAll);
        return "mail";
    }
    @RequestMapping("delete")
    public void delete(PrintWriter out,String mid ) {
        Integer integer = mailSerivce.deleteMail(Integer.parseInt(mid));
       if(integer == 1){
           out.print(1);
       }else{
           out.print(2);
       }

    }
}
